package com.bell.atensettings;

import java.util.ArrayList;

import com.bell.atensettings.bean.DisplayItems;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.DispList;
import android.view.DispList.DispFormat;
import android.view.DisplayManagerAw;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DisplaySettings extends Activity implements OnItemClickListener
{
    private Context mContext;
    private ImageView bridgeImage;
    private View mSelectView;
    private TextView mTitleView;
    private ListView mListView;
    private TextView mTipView;
    private ItemAdapter mItemAdapter;
    private ArrayList<DisplayItems> mDisplayItems;
    private DisplayManagerAw mDisplayManager;
    private int mIndex = 0;

    private void initTVoutMode()
    {
        mDisplayItems = new ArrayList<DisplayItems>();
        String curMode = Settings.System.getString(getContentResolver(), Settings.System.DISPLY_OUTPUT_FORMAT);
        String[] outMode = getResources().getStringArray(R.array.hdmi_mode_entries);
        mIndex = findIndexOfEntry(curMode, outMode);

        for (int index = 0; index < outMode.length; index++)
        {
            DisplayItems displayItem = new DisplayItems();

            displayItem.setName(outMode[index]);
            displayItem.setData((index + 2) + "");
            mDisplayItems.add(displayItem);
        }
    }

    private int findIndexOfEntry(String texts, String[] modeList)
    {
        int index = 0;

        if (!TextUtils.isEmpty(texts) && modeList != null)
        {
            while (index < modeList.length)
            {
                if (modeList[index].equals(texts))
                    return index;
                index++;
            }
        }
        return modeList.length - 1;
    }

    private void initView()
    {
        initTVoutMode();

        mTitleView = (TextView) findViewById(R.id.setting_title);
        mTipView = (TextView) findViewById(R.id.tip);
        mListView = (ListView) findViewById(R.id.listview);
        mItemAdapter = new ItemAdapter(mDisplayItems);

        mTitleView.setText(getIntent().getStringExtra("title"));
        mTipView.setVisibility(View.VISIBLE);
        mListView.setAdapter(mItemAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);

        setContentView(R.layout.public_settings);
        mDisplayManager = (DisplayManagerAw) getSystemService(Context.DISPLAY_SERVICE_AW);
        mContext = this;

        initView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        String hdmi_out_entries = mDisplayItems.get(position).getName();
        String hdmi_out_values = mDisplayItems.get(position).getData();
        ArrayList<DispFormat> dispList = DispList.getDispList();
        DispFormat dispFormat = dispList.get(Integer.parseInt(hdmi_out_values) - 1);
        
        mDisplayManager.setDisplayParameter(0, dispFormat.mOutputType, dispFormat.mFormat);
        mDisplayManager.setDisplayOutputType(0, dispFormat.mOutputType, dispFormat.mFormat);
        Settings.System.putString(getContentResolver(), Settings.System.DISPLY_OUTPUT_FORMAT, hdmi_out_entries);
        finish();

        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

    public class ItemAdapter extends BaseAdapter
    {
        private ArrayList<DisplayItems> mItem;

        public ItemAdapter(ArrayList<DisplayItems> items)
        {
            this.mItem = items;
        }

        public int getCount()
        {
            return this.mItem.size();
        }

        public Object getItem(int position)
        {
            return this.mItem.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            DisplayItems dispItem = (DisplayItems) this.mItem.get(position);

            if (convertView == null)
                convertView = ((LayoutInflater) mContext.getSystemService("layout_inflater")).inflate(
                        R.layout.list_display_item, null);
            convertView.setBackgroundResource(R.drawable.listnormal);
            ImageView signView = (ImageView) convertView.findViewById(R.id.img_sign);
            TextView tv_prop = (TextView) convertView.findViewById(R.id.tv_data);
            if (position == mIndex)
            {
                bridgeImage = signView;
                signView.setImageResource(R.drawable.choose_icon);
            }
            else
            {
                signView.setImageDrawable(null);
            }
            tv_prop.setText(dispItem.getName());
            convertView.setTag(signView);
            return convertView;
        }
    }

}
